// import initSqlJs from '@jlongster/sql.js';
// import { SQLiteFS } from 'absurd-sql';
// import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';

// async function init() {
// 	let SQL = await initSqlJs({ locateFile: file => file });
// 	console.log('4444')
// 	let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
// 	SQL.register_for_idb(sqlFS);
// 	SQL.FS.mkdir('/sql');
// 	SQL.FS.mount(sqlFS, {}, '/sql');
// 	const path = '/sql/db.sqlite';
// 	if (typeof SharedArrayBuffer === 'undefined') {
// 		let stream = SQL.FS.open(path, 'a+');
// 		await stream.node.contents.readIfFallback();
// 		SQL.FS.close(stream);
// 	}
// 	let db = new SQL.Database(path, {
// 		filename: true
// 	});
	
// 	// You might want to try `PRAGMA page_size=8192;` too!
// 	db.exec(`PRAGMA journal_mode=MEMORY;`);
// 	// Your code
// 	return db
// }


// async function runQueries() {
// 	let db = await init();
// 	console.log(db,'999999')
// 	try {
// 		db.exec('CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)');
// 	} catch (e) {}

// }

// runQueries();

import initSqlJs from 'sql.js';
import sqlWasm from "!!url-loader?name=sql-wasm-[contenthash].wasm!sql.js/dist/sql-wasm.wasm";
import { SQLiteFS } from 'absurd-sql';
import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';

async function run() {
	console.log('0000')
  let SQL = await initSqlJs({ 
	  locateFile: file => sqlWasm
	});
  console.log(SQL,'1111')
  let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
  SQL.register_for_idb(sqlFS);
  SQL.FS.mkdir('/sql');
  SQL.FS.mount(sqlFS, {}, '/sql');

  const path = '/sql/db.sqlite';
  if (typeof SharedArrayBuffer === 'undefined') {
    let stream = SQL.FS.open(path, 'a+');
    await stream.node.contents.readIfFallback();
    SQL.FS.close(stream);
  }

  let db = new SQL.Database(path, { filename: true });
  // You might want to try `PRAGMA page_size=8192;` too!
  db.exec(`
    PRAGMA journal_mode=MEMORY;
  `);

   // Your code
}


async function runQueries() {
	let db = await run();
	console.log(db,'999999')
	try {
		db.exec('CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)');
	} catch (e) {}

}

runQueries();


